package bairui.international.console.web.controller.prize.nine;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.project.commons.util.Util_FileDeal;
import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.lianjing.sh.validate.spring.exception.ValidateObjectException;

import bairui.international.model.domain.jpa.TB_PrizeItem;
import bairui.international.service.Service_PrizeItem;

/**
 *
 * 奖品管理
 *
 * @generated
 * 
 * @see STest_C_Prize_Nine_ItemManage_Edit 测试
 *
 */
@Controller
public class C_Prize_Nine_ItemManage_Edit
 extends SC_Prize_Nine_ItemManage_Edit 
{
	
	@Autowired private Service_PrizeItem service_PrizeItem;
	
	/**
	 * 编辑
	 */
	@Action(Edit.class)
	@PostMapping(Edit.RequestMappingValue)
	public @FomatOutput @ResponseBody Object edit(
		@Valid Edit.InputModel input, 
		@PathVariable(name=Edit.PathVariableName.PrizeId) long prizeId, 
		@PathVariable(name=Edit.PathVariableName.Id) long id
	) throws Exception {
		// 验证
		validate(input.gainRate());
		// 更新数据
		TB_PrizeItem bean = service_PrizeItem.updateWithNonNullProperties(id, input);
		// 更新图片
		Util_FileDeal.updateFile(input.getFileUrlFile(), bean.getFileUrl(), pathConfig.getPosition_Image_PrizeItem());
		//
		return null;
	}
	
	/**
	 * 验证中奖概率最多保留一位小数
	 */
	private static int validate(double rate) throws ValidateObjectException{
		String s = rate + "";	// 12345 -> 12345.0
//		System.out.println(s + "====" + s.length() + "====" + s.indexOf("."));
		int position = s.length() - s.indexOf(".") -1;
		if(position > 1){
			throw new ValidateObjectException("object", "", "中奖概率最多保留一位小数");
		}
		return position;
	}
	
//	public static void main(String[] args) throws ValidateObjectException {
//		int s = validate(12345);
//		System.out.println(s);
//	}

}
